package com.tbit.main.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tbit.main.constant.enums.MachineAlarmEnum;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.util.*;


/**
 * @Author: chen
 * @Description: 创蓝智云短信实名工具类
 * @Date: 创建于 15:31 2021/6/18
 */
@Slf4j
public class ClzySmsAuthUtil {

    /**
     * 实名认证签名校验地址
     */
    private static String URLV2 = "https://api.253.com/open/idcard/id-card-auth/vs";
    /**
     * 实名认证普通地址
     */
    private static String URL = "https://api.253.com/open/idcard/id-card-auth";

    /**
     * 国内短信发送
     *
     * @param account  用户名
     * @param password 密码
     * @param msg      短信内容 模板需要完整内容配置，签名需要申请
     * @param phone    手机号码，多个用英文逗号分割
     * @return
     */
    public static boolean sendSms(String account, String password, String msg, String phone) {
        String url = "http://smssh1.253.com/msg/v1/send/json";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("account", account);
        jsonObject.put("password", password);
        jsonObject.put("msg", msg);
        jsonObject.put("phone", phone);
        String json = jsonObject.toJSONString();
        LogUtil.info("创蓝：发送短信：" + json);
        try {
            String tmp = HttpClientUtil.httpJson(url, json);
            LogUtil.info("创蓝：发送短信结果:" + tmp);
            JSONObject jsonObject1 = JSON.parseObject(tmp);
            String errcode = jsonObject1.getString("code");
            if ("0".equals(errcode)) {
                return true;
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }


    /**
     * 生成加密字符串
     *
     * @param requestMap
     * @return
     */
    private static String requestMap2Str(Map<String, Object> requestMap) {
        List<String> list = new ArrayList<>(requestMap.keySet());
        Collections.sort(list);
        StringBuilder stringBuilder = new StringBuilder();
        for (String key : list) {
            stringBuilder.append(key).append(requestMap.get(key));
        }
        return stringBuilder.toString();
    }

    /**
     * 使用这个三个字段进行加密验签
     *
     * @param appId
     * @param name
     * @param idNum
     * @return
     */
    private static Map<String, Object> getPostData(String appId, String name, String idNum) {
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("idNum", idNum);
        requestMap.put("name", name);
        requestMap.put("appId", appId);
        return requestMap;
    }

    /**
     * 生成加密字符串,并用Base64进行编码
     *
     * @param encryptText
     * @param encryptKey
     * @return
     */
    public static String hmacSHA1Encrypt(String encryptText, String encryptKey) {
        byte[] result = null;
        try {
            //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
            SecretKeySpec signinKey = new SecretKeySpec(encryptKey.getBytes("UTF-8"), "HmacSHA1");
            //生成一个指定 Mac 算法 的 Mac 对象
            Mac mac = Mac.getInstance("HmacSHA1");
            //用给定密钥初始化 Mac 对象
            mac.init(signinKey);
            //完成 Mac 操作
            byte[] rawHmac = mac.doFinal(encryptText.getBytes("UTF-8"));
            result = Base64.getEncoder().encode(rawHmac);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        if (null != result) {
            return new String(result);
        } else {
            return null;
        }
    }


    public static void main(String[] args) {
        //sendSms("https://smssh1.253.com/msg/send/json","YZM7705342","0678Mokvcqa193","【大量出行】您正在申请手机注册,验证码为:123456,5分钟内有效!","15079017029");
        //nameAuthBySign("pRdcwLDf","kzpSGkEh","方旭帆","330821199411196013");

        //    String key ="account=dswew2er&password=23123131";
        //    String [] strings = key.split("&");
        //    String account = strings[0].substring(strings[0].indexOf("=")+1);
        //    String password = strings[1].substring(strings[1].indexOf("=")+1);
        //    System.out.println(account);
        //    System.out.println(password);

        //sendSms("N5440710","GU7X8oI6202408","【去选出行】您正在申请手机注册,验证码为:123456,5分钟内有效!","15238785583");
        //sendSms("N5440710","GU7X8oI6202408","您的骑行订单静止超过"+10+"分钟未还车，请及时还车，避免产生额外的费用。","15238785583");
        sendSms("N3303217", "RfynxSHCOpdfb3", "资产安全预警提醒：车辆预警：陆丰市碣石镇车辆61002716、61002886、61003273产生" + MachineAlarmEnum.MACHINE_ALARM_GEO.getAlarmDesc() + "异常，请及时处理", "15238785583");
        //sendSms("N5440710","GU7X8oI6202408","资产安全预警提醒：车辆预警："+5+"车辆"+147+"产生"+2+"异常，最后扫码地址为"+"asdfasf"+"请及时处理","15238785583");
        //sendSms("N5440710","GU7X8oI6202408","资产安全预警提醒：车辆预警："+5+"车辆"+258+"产生"+3+"异常，请及时处理","15238785583");
    }
}
